package com.huatai.exchange.integration.base.impl;

import com.alibaba.fastjson.JSON;
import com.faneui.common.exception.BusinessException;
import com.faneui.integration.http.HttpClientUtils;
import com.google.common.collect.Maps;
import com.huatai.base.facade.bean.dict.AppDictVo;
import com.huatai.base.facade.bean.dict.QueryAppDictVoListResp;
import com.huatai.base.facade.bean.dict.QueryAppDictVoResp;
import com.huatai.exchange.integration.base.AbstractBaseService;
import com.huatai.exchange.integration.base.service.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DictServiceImpl extends AbstractBaseService implements DictService {

	@Override
	public AppDictVo queryDict(String keyNo, String keyValue) throws BusinessException {
		String url = baseUrl + "appdict/queryAppDictVo";
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("keyNo", keyNo);
		paramMap.put("keyValue", keyValue);
		String result = HttpClientUtils.query(url, paramMap);
		QueryAppDictVoResp resp = JSON.parseObject(result, QueryAppDictVoResp.class);
		return resp.getAppDictVo();
	}

	@Override
	public List<AppDictVo> queryDictList(String keyNo) throws BusinessException {
		return queryDictList(keyNo, null);
	}

	@Override
	public List<AppDictVo> queryDictList(String keyNo, String parentId) throws BusinessException {
		String url = baseUrl + "appdict/queryAppDictVoList";
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("keyNo", keyNo);
		if (StringUtils.isNotBlank(parentId))
			paramMap.put("parentId", parentId);
		String result = HttpClientUtils.query(url, paramMap);
		QueryAppDictVoListResp resp = JSON.parseObject(result, QueryAppDictVoListResp.class);
		return resp.getAppDictVos();
	}

	@Override
	public void modifyDict(String dictId, String keyValue) throws BusinessException {
		AppDictVo vo = new AppDictVo();
		vo.setDictId(dictId);
		vo.setKeyValue(keyValue);
		modifyDict(vo);
	}

	@Override
	public void modifyDict(AppDictVo appDictVo) throws BusinessException {
		String url = baseUrl + "appdict/modifydict";
		Map<String, Object> param = Maps.newHashMap();
		if (StringUtils.isNotBlank(appDictVo.getDictId()))
			param.put("dictId", appDictVo.getDictId());
		if (StringUtils.isNotBlank(appDictVo.getKeyNo()))
			param.put("keyNo", appDictVo.getKeyNo());
		if (StringUtils.isNotBlank(appDictVo.getKeyValue()))
			param.put("keyValue", appDictVo.getKeyValue());
		if (StringUtils.isNotBlank(appDictVo.getCaption()))
			param.put("caption", appDictVo.getCaption());
		if (StringUtils.isNotBlank(appDictVo.getParentId()))
			param.put("parentId", appDictVo.getParentId());
		HttpClientUtils.post(url, param);
	}

	@Override
	public String getKeyValue(String keyNo) throws BusinessException {
		return queryDictList(keyNo).get(0).getKeyValue();
	}
}
